//
//  ViewControllerForClickSearch.m
//  ProjectA
//
//  Created by dllo on 16/2/28.
//  Copyright © 2016年 刘旺辉. All rights reserved.
//

#import "ModelForSearchUsers.h" //M
#import "ModelForSearchTrips.h"
#import "ModelForSearchPlaces.h"

#import "ViewForSearchHeader.h" //V
#import "CellForSearchTrips.h"
#import "CellForSearchUsers.h"
#import "ViewForSearchFooter.h"

#import "ViewControllerForSearch.h" //C


@interface ViewControllerForSearch ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) NSMutableArray *arrForPlaces;
@property (nonatomic, strong) NSMutableArray *arrForTrips;
@property (nonatomic, strong) NSMutableArray *arrForUsers;
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation ViewControllerForSearch
- (void)dealloc {
    
    self.tableView.dataSource = nil;
    self.tableView.delegate = nil;
    
    [_arrForPlaces release];
    [_arrForTrips release];
    [_arrForUsers release];
    [_search release];
    [_tableView release];
    [super dealloc];
}
- (void)setNightDayModel {
    [super setNightDayModel];

    self.barBackgroundView.backgroundColor = RGBA(52, 52, 52, 1);
}
- (void)setLightDayModel {
    [super setLightDayModel];
    
    self.barBackgroundView.backgroundColor = RGBA(44, 211, 209, 1);
}
- (void)viewWillAppear:(BOOL)animated {
    [self.tableView reloadData];
    if (ISNIGHT) {
        [self setNightDayModel];
    } else {
        [self setLightDayModel];
    }
}
- (void)viewWillDisappear:(BOOL)animated {
    self.barBackgroundView.backgroundColor = L_COLOR_CLEAR;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self handleData];
    [self settingsNavigationController];
    [self createTableView];
}

- (void)createTableView {
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.tableView];
    [_tableView release];
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    [self.tableView registerClass:[ViewForSearchHeader class] forHeaderFooterViewReuseIdentifier:@"poolForHeader"];
    [self.tableView registerClass:[ViewForSearchFooter class] forHeaderFooterViewReuseIdentifier:@"poolForFooter"];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"poolForPlaces"];
    [self.tableView registerClass:[CellForSearchTrips class] forCellReuseIdentifier:@"poolForTrips"];
    [self.tableView registerClass:[CellForSearchUsers class] forCellReuseIdentifier:@"poolForUsers"];
  
   // self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;  //去掉cell的分割线
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (0 == section) return self.arrForPlaces.count;
    if (1 == section) return self.arrForTrips.count;
    else              return self.arrForUsers.count;

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (0 == indexPath.section) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForPlaces"];
        cell.backgroundColor = [UIColor clearColor];
        ModelForSearchPlaces *model = [self.arrForPlaces objectAtIndex:indexPath.row];
        
        NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:model.name];
        [attributedStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:[model.name rangeOfString:self.search]];
        if (ISNIGHT) {
            cell.textLabel.textColor = [UIColor whiteColor];
        } else {
            cell.textLabel.textColor = [UIColor blackColor];
        }

        cell.textLabel.attributedText = attributedStr;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
        
    }
    if (1 == indexPath.section) {
        CellForSearchTrips *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForTrips"];
        cell.backgroundColor = [UIColor clearColor];
      
        [cell passData:[self.arrForTrips objectAtIndex:indexPath.row] withSearch:self.search];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
    }
    else {
        
        CellForSearchUsers *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForUsers"];
        cell.backgroundColor = [UIColor clearColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        ModelForSearchUsers *model = [self.arrForUsers objectAtIndex:indexPath.row];
        [cell passData:model withSearch:self.search];
        return cell;
        
    }

}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    ViewForSearchFooter *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"poolForFooter"];
    [UIView setColorWithView:view.contentView nightColor:RGBA(52, 52, 52, 1) dayColorWithRed:255 green:251 blue:222 alpha:1];

    if (0 == section) {
        if (self.arrForPlaces.count) {
            view.label.text = @"";
            view.hidden = YES;
        } else {
            view.label.text = @"暂无该记录";
        }
    }
    if (1 == section) {
        if (self.arrForTrips.count) {
            view.label.text = @"";
            view.hidden = YES;
        } else {
            view.label.text = @"暂无该记录";
        }
    }
    else {
        if (self.arrForUsers.count) {
            view.label.text = @"";
            view.hidden = YES;
        } else {
            view.label.text = @"暂无该记录";
        }
    }
    return view;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    ViewForSearchHeader *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"poolForHeader"];
    if (0 == section) {
        view.label.text = @"相关目的地";
        view.contentView.backgroundColor = [UIColor clearColor];
        [UIView setColorWithView:view.contentView nightColor:RGBA(52, 52, 52, 1) dayColorWithRed:255 green:251 blue:222 alpha:1];
        return view;
    }
    if (1 == section) {
        view.label.text = @"相关游记 & 故事集";
        view.contentView.backgroundColor = [UIColor clearColor];
        [UIView setColorWithView:view.contentView nightColor:RGBA(52, 52, 52, 1) dayColorWithRed:255 green:251 blue:222 alpha:1];
        return view;
    }
    else {
        view.label.text = @"相关面粉";
        view.contentView.backgroundColor = [UIColor clearColor];
        [UIView setColorWithView:view.contentView nightColor:RGBA(52, 52, 52, 1) dayColorWithRed:255 green:251 blue:222 alpha:1];
        return view;
    }
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
  
    return 20;

}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 44;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (0 == indexPath.section) {
        return 44;
    }
    if (1 == indexPath.section) {
        return 100;
    }
    else {
        return 44;
    }
}

- (void)settingsNavigationController {
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-fanhui(heise)"] style:UIBarButtonItemStylePlain target:self action:@selector(houtuiAction:)];
    self.navigationItem.title = @"搜索结果";
    
}

- (void)houtuiAction:(UIBarButtonItem *)button {
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)handleData {
    self.arrForPlaces = [NSMutableArray array];
    self.arrForTrips = [NSMutableArray array];
    self.arrForUsers = [NSMutableArray array];
    
    NSString *url = [NSString stringWithFormat:@"http://api.breadtrip.com/v2/search/?key=%@&sign=cc05d4d923f631f9bfaee151afa05ad9", self.search];
    
    [NetWorkHandler networkingWithUrl:url comleteHandle:^(id result, NSData *data) {
        
        NSDictionary *dicForData = [result objectForKey:@"data"];
        NSArray *users = [dicForData objectForKey:@"users"];
        NSArray *trips = [dicForData objectForKey:@"trips"];
        NSArray *places = [dicForData objectForKey:@"places"];
        
        if (users.count) {
            
            for (NSDictionary *dic in users) {
                ModelForSearchUsers *modelForUsers = [[ModelForSearchUsers alloc] init];
                [modelForUsers setValuesForKeysWithDictionary:dic];
                [self.arrForUsers addObject:modelForUsers];
                [modelForUsers release];
            }
        }
        if (trips.count) {
            for (NSDictionary *dic in trips) {
                ModelForSearchTrips *modelForTrips = [[ModelForSearchTrips alloc] init];
                [modelForTrips setValuesForKeysWithDictionary:dic];
                [self.arrForTrips addObject:modelForTrips];
                [modelForTrips release];
            }
        }
        if (places.count) {
            for (NSDictionary *dic in places) {
                ModelForSearchPlaces *modelForPlaces = [[ModelForSearchPlaces alloc] init];
                [modelForPlaces setValuesForKeysWithDictionary:dic];
                [self.arrForPlaces addObject:modelForPlaces];
                [modelForPlaces release];
            }
        }

        [self.tableView reloadData];
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
